package io.renren.utils;

/**
 * @author DuJinliang
 * 2022/4/13
 */
public class ResultResponse<T> {
    private Integer code;
    private String message;
    private T data;

    public ResultResponse(){
    }

    protected static <T> ResultResponse<T> build(T data){
        ResultResponse<T> resultResponse = new ResultResponse<>();
        if (data != null){
            resultResponse.setData(data);
        }
        return resultResponse;
    }

    public static <T> ResultResponse<T> build(T data, ResultCodeEnum resultCodeEnum){
        ResultResponse<T> resultResponse = build(data);
        resultResponse.setCode(resultCodeEnum.getCode());
        resultResponse.setMessage(resultCodeEnum.getMessage());
        return resultResponse;
    }

    public static <T> ResultResponse<T> build(Integer code, String message){
        ResultResponse<T> resultResponse = build(null);
        resultResponse.setCode(code);
        resultResponse.setMessage(message);
        return resultResponse;
    }

    public static <T> ResultResponse<T> ok(){
        return ResultResponse.ok(null);
    }

    public static <T> ResultResponse<T> ok(T data){
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> ResultResponse<T> fail(){
        return fail(null);
    }

    public static <T> ResultResponse<T> fail(T data){
        return build(data, ResultCodeEnum.FAIL);
    }

    public ResultResponse<T> code(Integer code){
        this.setCode(code);
        return this;
    }

    public ResultResponse<T> message(String message){
        this.setMessage(message);
        return this;
    }

    public boolean isOk(){
        if (this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()){
            return true;
        }
        return false;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
